<template>
  <div>
    <div>
      <el-form inline>
        <el-form-item label="关键字">
          <el-input v-model="searchBulletinDto.keyWord" placeholder="输入搜索内容"/>
        </el-form-item>
        <el-form-item label="优先级">
          <el-select v-model="searchBulletinDto.level">
            <el-option label="全部" :value="-1"/>
            <el-option label="紧急" :value="0"/>
            <el-option label="高" :value="1"/>
            <el-option label="普通" :value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button>查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button color="rgba(4, 114, 53, 0.8)" @click="addDialog=true">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table :data="announceList" show-overflow-tooltip>
        <el-table-column prop="id" label="编号"/>
        <el-table-column prop="name" label="名称"/>
        <el-table-column prop="level" label="优先级"/>
        <el-table-column prop="title" label="标题"/>
        <el-table-column prop="content" label="内容"/>
        <el-table-column label="状态">
          <template #default="scope">
            <el-tag v-if="scope.row.status===0" type="success">正常</el-tag>
            <el-tag v-if="scope.row.status===1">撤销</el-tag>
            <el-tag v-if="scope.row.status===2">删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="targetType" label="通告目标"/>
        <el-table-column label="操作">
          <template #default="item">
            <el-text class="mx-1" type="danger" @click="openDeletePopUps(item.row) ">删除</el-text>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination background layout="prev, pager, next"
                     hide-on-single-page
                     v-model:page-size="searchBulletinDto.pageSize"
                     v-model:current-page="searchBulletinDto.pageNum"
                     :total="searchBulletinDto.total"/>
    </div>
  </div>
  <!--新增弹窗-->
  <div>
    <el-dialog
        v-model="addDialog"
        title="新增"
        width="50%"
        @close="fromRef.resetFields()"
        draggable
    >
      <el-form :inline="true" :model="addBulletin" ref="fromRef" label-width=80 :rules="rules">
        <el-form-item label="优先级" prop="level">
          <el-select v-model="addBulletin.level">
            <el-option label="紧急" value="0"/>
            <el-option label="高" value="1"/>
            <el-option label="普通" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="addBulletin.title" placeholder="请输入标题" clearable/>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="addBulletin.name" placeholder="请输入名称" clearable/>
        </el-form-item>
        <el-form-item label="内容" prop="content" type="textarea">
          <el-input v-model="addBulletin.content" placeholder="请输入内容" clearable/>
        </el-form-item>
        <el-form-item label="通告目标" prop="targetType">
          <el-input v-model="addBulletin.targetType" placeholder="请输入目标对象" clearable/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="addDialog = false">取消</el-button>
        <el-button color="rgba(4, 114, 53, 0.8)" @click="submitAdd(fromRef)">
          确定
        </el-button>
      </span>
      </template>
    </el-dialog>
  </div>
  <!--  修改弹窗-->

  <div>
    <el-dialog
        v-model="updateDialog"
        title="修改"
        width="50%"
        @close="fromRef.resetFields()"
        draggable
    >
      <div class="add_box">
        <el-form :model="modifyBulletin" ref="fromRef" label-width="80">
          <el-form-item label="标题" prop="title">
            <el-input v-model="modifyBulletin.title" clearable/>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input v-model="modifyBulletin.content" clearable/>
          </el-form-item>
          <el-form-item label="优先级" prop="level">
            <el-input v-model="modifyBulletin.level" clearable/>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-input v-model="modifyBulletin.status" placeholder="请输入排序规则" clearable/>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="addDialog = false">取消</el-button>
        <el-button color="rgba(4, 114, 53, 0.8)" @click="submitUpdate">
          保存
        </el-button>
      </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import {ref, reactive, onMounted} from 'vue'
import {getBulletinList, createBulletin} from '@/api/home/communication/annouce'

let searchBulletinDto = reactive({
  keyWord: '',
  level: -1,
  pageNum: 1,
  pageSize: 5,
  status: -1,
  total: 0
})
let announceList = ref([])
let addDialog = ref(false)
let fromRef = ref({})
let modifyBulletin = ref({})
let updateDialog = ref(false)

let addBulletin = reactive({
  level: '',
  title: '',
  content: '',
  status: 0,
  targetType: '',
  pageNum: 1,
  pageSize: 5,
  total: 0
})


function getBulletins() {
  getBulletinList(searchBulletinDto).then(res => {
    announceList.value = res.data.records
    searchBulletinDto.total = res.data.total-0
  })
}

function submitAdd(r) {
  createBulletin(addBulletin).then(() => {
    getBulletins()
  }).finally(() => {
    addDialog.value = true
  })
}

function submitUpdate() {

}

onMounted(() => {
  getBulletins()
})
</script>

<style scoped>

</style>
